import { commonLS } from '../../utils/common';
import { DELETE_USERINFO_TYPE, UPDATE_USERINFO_TYPE } from './types';

const reducer = (
  state = (() => {
    const [nameErr, adminname] = commonLS.get('adminname')
    const [tokenErr, token] = commonLS.get('token')
    const [checkedkeysErr, checkedkeys] = commonLS.get('checkedkeys')
    const [roleErr, role] = commonLS.get('role')
    return {
      adminname: nameErr ? '' : adminname,
      token: tokenErr ? '' : token,
      checkedkeys: checkedkeysErr ? '' : checkedkeys,
      role: roleErr ? '' : role,
    }
  })(),
  action = {},
) => {
  const newState = { ...state }
  const { type, payload } = action
  switch (type) {
    case UPDATE_USERINFO_TYPE:
      return { ...newState, ...payload }
    case DELETE_USERINFO_TYPE:
      return {
        adminname: '', token: '', checkedkeys: [], role: 1,
      }
    default:
      break;
  }
  return newState
}

export default reducer;
